<?php
/**
 * @file cooperationdefine.php
 * @author CaoQiCheng
 * @date 2016年4月26日 下午17:16:11
 * @brief 
 */

namespace haibao\cms\data\cooperation;

class CooperationDefine extends \haibao\cms\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\cms\model\data\cooperation\cooperationdefine');
    }
    
    public function getCooperationDefineLists($query){
        return $this->getAll($query);
    }

    /**
     * 保存合作信息
     */
    public function saveCooperationInfo($model,$cooperationBrandArr,$cooperationEventArr,$cooperationFeatureArr,$videolist,$cooperationRelPrincipal,$cooperationOutResource,$cooperationArtilce,$cooperationVvNum,$cooperationCost,$cooperationColumnRel){
    	$this->beginTranscation();
    	try {
    		if($model->Id){
    			$this->save($model);
    		}else {
    			$this->add($model);
    		}
    	
			$this->saveCooperationBrand($model->Id, $cooperationBrandArr);
			$this->savaCooperationEvent($model->Id, $cooperationEventArr);
			$this->saveCooperationFeature($model->Id, $cooperationFeatureArr);
			$this->saveCooperationVideo($model->Id,$videolist);
			$this->saveCooperationRelPrincipal($model->Id, $cooperationRelPrincipal);
			$this->saveCooperationOutResource($model->Id, $cooperationOutResource);
			$this->saveCooperationTypes($model->Id,$model->CooperationCategory);
			$this->saveCooperationArticle($model->Id,$cooperationArtilce);
			$this->saveCooperationVvNum($model->Id, $cooperationVvNum);
			$this->saveCooperationCost($model->Id, $cooperationCost);
			$this->saveCooperationColumnRel($model->Id, $cooperationColumnRel);
			if(isset($model->CooperatorId[\haibao\cms\model\data\cooperation\CooperationDefine::TYPE_CELEBRITY])){
				$celebrityIds = $model->CooperatorId[\haibao\cms\model\data\cooperation\CooperationDefine::TYPE_CELEBRITY];
				$this->saveCooperationRel($model->Id,$celebrityIds,\haibao\cms\model\data\cooperation\CooperationDefine::TYPE_CELEBRITY);
			}
			if(isset($model->CooperatorId[\haibao\cms\model\data\cooperation\CooperationDefine::TYPE_STAR])){
				$starIds = $model->CooperatorId[\haibao\cms\model\data\cooperation\CooperationDefine::TYPE_STAR];
				$this->saveCooperationRel($model->Id,$starIds,\haibao\cms\model\data\cooperation\CooperationDefine::TYPE_STAR);
			}
			if(isset($model->CooperatorId[\haibao\cms\model\data\cooperation\CooperationDefine::TYPE_DESIGNER])){
				$starIds = $model->CooperatorId[\haibao\cms\model\data\cooperation\CooperationDefine::TYPE_DESIGNER];
				$this->saveCooperationRel($model->Id,$starIds,\haibao\cms\model\data\cooperation\CooperationDefine::TYPE_DESIGNER);
			}

    	} catch (\Exception $e) {
    		$this->rollback();
    		throw $e;
    	}
    	$this->endTranscation();
    }
    /**
     * 增加合作资源类型
     */
    public function saveCooperationOutResource($cooperationId,$cooperationOutResource){
    	if(!$cooperationId){
    		return false;
    	}
    	$this->query('delete from `CooperationOutResource` where CooperationId = %s', array(
    			$cooperationId
    	));
    	if(isset($cooperationOutResource) && $cooperationOutResource){
    		foreach($cooperationOutResource as $key=>$val){
    			$val->CooperationId = $cooperationId;
    		}
    		$this->addAll($cooperationOutResource);
    	}
    }
    
    /**
     * 增加合作vv数
     */
    public function saveCooperationVvNum($cooperationId,$cooperationVvNum){
    	if(!$cooperationId){
    		return false;
    	}
    	$this->query('delete from `CooperationProperty` where CooperationId = %s', array(
    			$cooperationId
    	));
    	$sql = 'insert into CooperationProperty(CooperationId,VideoViewNum) values (%s,%s) ';
    	$this->query($sql,array($cooperationId,$cooperationVvNum));
    }
    
    /**
     * 增加合作花费
     */
    public function saveCooperationCost($cooperationId,$cooperationCost){
    	if(!$cooperationId){
    		return false;
    	}
    	$this->query('delete from `CooperationCost` where CooperationId = %s', array(
    			$cooperationId
    	));
    	$sql = 'insert into CooperationCost(CooperationId,TotalCost,MakeupCost,CameraCost,ModelCost,ArtistCost,FieldCost,EquipmentCost,OtherCost) values (%s,%s,%s,%s,%s,%s,%s,%s,%s) ';
    	$this->query($sql,array($cooperationId,$cooperationCost['TotalCost'],$cooperationCost['MakeupCost'],$cooperationCost['CameraCost'],$cooperationCost['ModelCost'],$cooperationCost['ArtistCost'],$cooperationCost['FieldCost'],$cooperationCost['EquipmentCost'],$cooperationCost['OtherCost']));
    }
    
    /**
     * 添加栏目列表
     */
    public function saveColumn($columnModel,$isEdit){
    		if(!$columnModel){
    			return false;
    		}
    		if($isEdit){
    			$this->save($columnModel);
    		}else {
    			$this->add($columnModel);
    		}
    		
    		return $columnModel;
    }
    /**
     * 添加负责人
     */
    public function savePrincipal($principalModel,$isEdit){
    	if(!$principalModel){
    		return false;
    	}
    	if($isEdit){
    		$this->save($principalModel);
    	}else {
    		$this->addAll($principalModel);
    	}
    	
    	return $principalModel;
    }
    /**
     * 删除负责人
     */
    public function delPrincipal($principalId){
    	if(!$principalId){
    		return false;
    	}
    	$this->query('delete from `CooperationPrincipal` where Id = %s', array(
    			$principalId
    	));
    }
    
    /**
     * 增加合作的负责人
     */
    public function saveCooperationRelPrincipal($cooperationId,$cooperationRelPrincipal){
    	if(!$cooperationId){
    		return false;
    	}
    	$this->query('delete from `CooperationRelPrincipal` where CooperationId = %s', array(
    			$cooperationId
    	));
		$sql = 'insert into CooperationRelPrincipal(CooperationId,PrincipalType,UserName) values ';
		$arrSql = $arrValues = array();
		foreach($cooperationRelPrincipal as $key=>$value){
			if($value){
				$userNames = explode(',',$value);
				foreach($userNames as $userName){
					array_push($arrSql, '(%s,%s,%s)');
					array_push($arrValues, $cooperationId);
					array_push($arrValues, $key+1);
					array_push($arrValues, $userName);
				}
			}
		}
		if($arrSql && $arrValues){
			$sql .= implode(',', $arrSql);
			$this->query($sql, $arrValues);
		}
    }
    
    
    /**
     * 增加合作视频id
     */
    public function saveCooperationVideo($cooperationId,$videolist){
    	if(!$cooperationId){
    		return false;
    	}
    	$this->query('delete from `CooperationVideo` where CooperationId = %s', array(
    			$cooperationId
    	));
    	if(isset($videolist) && $videolist){
    		$sql = 'insert into CooperationVideo(CooperationId,VideoId) values ';
    		$arrSql = $arrValues = array();
    	
    		foreach($videolist as $video){
    			array_push($arrSql, '(%s,%s)');
    			array_push($arrValues, $cooperationId);
    			array_push($arrValues, $video);
    		   }
    		if($arrSql && $arrValues){
    			$sql .= implode(',', $arrSql);
    			$this->query($sql, $arrValues);
    		}
    	}
    	
    }
    
    /**
     * 增加合作事件专题
     */
    public function saveCooperationFeature($cooperationId,$cooperationFeatureArr){
    	if(!$cooperationId){
    		return false;
    	}
    	$this->query('delete from `CooperationFeature` where CooperationId = %s', array(
    			$cooperationId
    	));
    	if(isset($cooperationFeatureArr) && $cooperationFeatureArr){
    		$sql = 'insert into CooperationFeature(CooperationId,FeatureId) values ';
    		$arrSql = $arrValues = array();
    		foreach($cooperationFeatureArr as $key=>$val){
    			array_push($arrSql, '(%s,%s)');
    			array_push($arrValues, $cooperationId);
    			array_push($arrValues, $val);
    		}
    		if($arrSql && $arrValues){
    			$sql .= implode(',', $arrSql);
    			$this->query($sql, $arrValues);
    		}
    	}
    }
    
    /**
     * 增加合作栏目关系依赖
     */
    public function saveCooperationColumnRel($cooperationId,$cooperationColumnRel){
    	if(!$cooperationId){
    		return false;
    	}
    	$this->query('delete from `CooperationColumnRel` where CooperationId = %s', array(
    			$cooperationId
    	));
    	if(isset($cooperationColumnRel) && $cooperationColumnRel){
    		$sql = 'insert into CooperationColumnRel(CooperationId,ColumnId) values ';
    		$arrSql = $arrValues = array();
    		foreach($cooperationColumnRel as $key=>$val){
    			array_push($arrSql, '(%s,%s)');
    			array_push($arrValues, $cooperationId);
    			array_push($arrValues, $val);
    		}
    		if($arrSql && $arrValues){
    			$sql .= implode(',', $arrSql);
    			$this->query($sql, $arrValues);
    		}
    	}
    }
    
    /**
     * 增加合作事件的关联
     */
    public function savaCooperationEvent($cooperationId,$cooperationEventArr){
    	if(!$cooperationId){
    		return false;
    	}
    	$this->query('delete from `CooperationEvent` where CooperationId = %s', array(
    			$cooperationId
    	));
    	if(isset($cooperationEventArr) && $cooperationEventArr){
    		$sql = 'insert into CooperationEvent(CooperationId,EventId) values ';
    		$arrSql = $arrValues = array();
    		foreach($cooperationEventArr as $key=>$val){
    			array_push($arrSql, '(%s,%s)');
    			array_push($arrValues, $cooperationId);
    			array_push($arrValues, $val);
    		}
    		if($arrSql && $arrValues){
    			$sql .= implode(',', $arrSql);
    			$this->query($sql, $arrValues);
    		}
    	}
    }
    
    /**
     * 增加合作的品牌关联
     */
    public function saveCooperationBrand($cooperationId,$cooperationBrandArr){
    	if(!$cooperationId){
    		return false;
    	}
    	$this->query('delete from `CooperationBrand` where CooperationId = %s', array(
    			$cooperationId
    	));
    	if(isset($cooperationBrandArr) && $cooperationBrandArr){
    		$sql = 'insert into CooperationBrand(CooperationId,BrandId) values ';
    		$arrSql = $arrValues = array();
    		foreach($cooperationBrandArr as $key=>$val){
    			array_push($arrSql, '(%s,%s)');
    			array_push($arrValues, $cooperationId);
    			array_push($arrValues, $val);
    		}
    		if($arrSql && $arrValues){
    			$sql .= implode(',', $arrSql);
    			$this->query($sql, $arrValues);
    		}
    	}
    }

	public function saveCooperationTypes($cooperationId,$types){
		if($cooperationId && $types){
			$this->query('delete from CooperationTypes WHERE CooperationId = %s',array($cooperationId));
			$insertSql = 'insert into CooperationTypes(CooperationId,Type) values ';
			$arrSql = $arrValues = array();
			foreach($types as $val){
				array_push($arrSql,'(%s,%s)');
				array_push($arrValues,$cooperationId);
				array_push($arrValues,$val);
			}
			if($arrSql && $arrValues){
				$insertSql .= implode(',',$arrSql);
				$this->query($insertSql,$arrValues);
			}
		}
	}

	public function saveCooperationRel($cooperationId,$stars,$type){
		$tables = array(
			\haibao\cms\model\data\cooperation\CooperationDefine::TYPE_CELEBRITY => 'CooperationRelCelebrity',
			\haibao\cms\model\data\cooperation\CooperationDefine::TYPE_STAR => 'CooperationRelStar',
			\haibao\cms\model\data\cooperation\CooperationDefine::TYPE_DESIGNER => 'CooperationRelDesigner',
		);
		$fileds = array(
			\haibao\cms\model\data\cooperation\CooperationDefine::TYPE_CELEBRITY => 'CelebrityId',
			\haibao\cms\model\data\cooperation\CooperationDefine::TYPE_STAR => 'StarId',
			\haibao\cms\model\data\cooperation\CooperationDefine::TYPE_DESIGNER => 'DesignerId',
		);
		$tableName = $tables[$type];
		$filedName = $fileds[$type];
		$this->query('delete from '.$tableName.' WHERE CooperationId = %s',array($cooperationId));
		if($cooperationId && $stars){
			$insertSql = 'insert into '.$tableName.'(CooperationId,'.$filedName.') values ';
			$arrSql = $arrValues = array();
			$stars = explode(',',$stars);
			foreach($stars as $val){
				array_push($arrSql,'(%s,%s)');
				array_push($arrValues,$cooperationId);
				array_push($arrValues,$val);
			}
			if($arrSql && $arrValues){
				$insertSql .= implode(',',$arrSql);
				$this->query($insertSql,$arrValues);
			}
		}
	}

	public function saveCooperationArticle($cooperationId,$articles){
		if($cooperationId && $articles){
			$this->query('delete from CooperationArticle WHERE CooperationId = %s',array($cooperationId));
			$insertSql = 'insert into CooperationArticle(CooperationId,ArticleId) values ';
			$arrSql = $arrValues = array();
			foreach($articles as $val){
				array_push($arrSql,'(%s,%s)');
				array_push($arrValues,$cooperationId);
				array_push($arrValues,$val);
			}
			if($arrSql && $arrValues){
				$insertSql .= implode(',',$arrSql);
				$this->query($insertSql,$arrValues);
			}
		}
	}

	public function saveCooperationDesigner($cooperationId,$designers){
		if($cooperationId && $designers){
			$this->query('delete from CooperationDesigner WHERE CooperationId = %s',array($cooperationId));
			$insertSql = 'insert into CooperationDesigner(CooperationId,DesignerId) values ';
			$arrSql = $arrValues = array();
			foreach($designers as $val){
				array_push($arrSql,'(%s,%s)');
				array_push($arrValues,$cooperationId);
				array_push($arrValues,$val);
			}
			if($arrSql && $arrValues){
				$insertSql .= implode(',',$arrSql);
				$this->query($insertSql,$arrValues);
			}
		}
	}
    
	/**
	 * 获取所有明星合作的表内内容
	 */
    public function getCooperationRelCelebrity($celebrityIds){
    	
    	$celebrityStr = implode(',',$celebrityIds);
        $sql = 'select CooperationId from CooperationRelCelebrity where CelebrityId in ('.$celebrityStr.')';
        $result = $this->query($sql,$celebrityIds);
        $celebrityIdArr = array();
        while($row = $result->fetch_assoc()){
           $celebrityIdArr[] = $row['CooperationId'];
        }
        return $celebrityIdArr;
    }
    
    public function delCooperationById($cooperationId){
    	
    	$this->removeById($cooperationId);
    	$sql = 'delete from CooperationArticle where CooperationId=%s';
    	$this->query($sql,array($cooperationId));
    	$sql = 'delete from CooperationBrand where CooperationId=%s';
    	$this->query($sql,array($cooperationId));
    	$sql = 'delete from CooperationEvent where CooperationId=%s';
    	$this->query($sql,array($cooperationId));
    	$sql = 'delete from CooperationFeature where CooperationId=%s';
    	$this->query($sql,array($cooperationId));
    	$sql = 'delete from CooperationOutResource where CooperationId=%s';
    	$this->query($sql,array($cooperationId));
    	$sql = 'delete from CooperationRelPrincipal where CooperationId=%s';
    	$this->query($sql,array($cooperationId));
    	$sql = 'delete from CooperationRelCelebrity where CooperationId=%s';
    	$this->query($sql,array($cooperationId));
    	$sql = 'delete from CooperationRelDesigner where CooperationId=%s';
    	$this->query($sql,array($cooperationId));
    	$sql = 'delete from CooperationRelStar where CooperationId=%s';
    	$this->query($sql,array($cooperationId));
    	$sql = 'delete from CooperationTypes where CooperationId=%s';
    	$this->query($sql,array($cooperationId));
    	$sql = 'delete from CooperationVideo where CooperationId=%s';
    	$this->query($sql,array($cooperationId));
    	
    }


}